(function () {

    function showalert(txt) {
        $("#modal-alert").find(".am-modal-bd").text(txt)
            .end()
            .modal("open");
    }


    $("#address-btn").click(function () {
        addAddress()
    })


    if (id != null && typeof id != "undefined" && id != "null") {
        var url = "/h5/getAddress?id=" + id;
        $.post(url, function (result) {
            $('#detail_address').val(result.address);
            $('#consignee').val(result.name);
            $('#postalcode').val(result.zip);
            $('#phone').val(result.phone);
            $('#id').val(result.id);
            $("#province_select").val(result.province);
            $("#province_select").val(result.city);
            $("#province_select").val(result.area);
        })
    }


    function addAddress() {
        var province = $('#province_select').val(),
            city = $('#city_select').val(),
            county = $('#county_select').val()
        if (province == null || province == "") {
            showalert('请选择省份');
            return;
        }
        if (city == null || city == "") {
            showalert('请选择市');
            return;
        }
        if (county == null || county == "") {
            showalert('请选择区(县)');
            return;
        }
        var detailAddress = $('#detail_address').val();
        if (!detailAddress) {
            showalert('详细地址不能为空');
            return;
        }
        var regPost = /^[0-9][0-9]{5}$/;
        var postalcode = $('#postalcode').val();
        if (postalcode && !regPost.test(postalcode)) {
            showalert('邮政编码格式错误');
            return;
        }
        var consignee = $('#consignee').val();
        var id = $('#id').val();
        if (consignee.length > 20 || consignee.length <= 0) {
            showalert('收货人长度不超过20个字符,且不为空');
            return;
        }

        var phone = $('#phone').val();
        var regMobile = /^1[3|4|5|6|7|8|9][0-9]{1}[0-9]{8}$/;
        var mobileResult = regMobile.test(phone);
        if (mobileResult == false) {
            showalert('手机号有误,请重新输入');
            return;
        }
        var user = JSON.parse(localStorage.getItem("user"));
        var url = '/h5/saveAddress';
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: url,
            data: {
                province: province,
                city: city,
                area: county,
                address: detailAddress,
                phone: phone,
                zip: postalcode,
                name: consignee,
                account: user.account,
                mobile: phone,
                id: id
            },
            success: function (res) {
                if (res.code = '000000') {
                    window.location.href = "../../app/settings/address-list.jsp"
                }


            },
            error: function (res) {

            }
        });

    }


    selectAreas();
    function selectAreas() {
        var url = "/h5/selectAreas";
        $.post(url, function (result) {
            var source = $('#area-template').html();
            var template = Handlebars.compile(source);
            var html = template(result.provinces);
            $("#province_select").empty();
            var option = "<option value=''>请选择</option>"
            $("#province_select").append(option);
            $("#province_select").append(html);


            var html = template(result.citys);
            $("#city_select").empty();
            var option = "<option value=''>请选择</option>"
            $("#city_select").append(option);
            $("#city_select").append(html);


            var html = template(result.areas);
            $("#county_select").empty();
            var option = "<option value=''>请选择</option>"
            $("#county_select").append(option);
            $("#county_select").append(html);

        })
    }


    $("#province_select").change(function () {
        var code = $(this).val();
        var url = "/h5/selectAreas";
        $.post(url, {
            provinceCode: code
        }, function (result) {
            var source = $('#city-template').html();
            var template = Handlebars.compile(source);
            var html = template(result.citys);
            $("#city_select").empty();
            var option = "<option value=''>请选择</option>"
            $("#city_select").append(option);
            $("#city_select").append(html);


        })

    })


    $("#city_select").change(function () {
        var code = $(this).val();
        var url = "/h5/selectAreas";
        $.post(url, {
            cityCode: code
        }, function (result) {
            var source = $('#country-template').html();
            var template = Handlebars.compile(source);
            var html = template(result.areas);
            $("#county_select").empty();
            var option = "<option value=''>请选择</option>"
            $("#county_select").append(option);
            $("#county_select").append(html);


        })

    })


})();



